package com.csoa.community.utils;

import java.io.File;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.widget.Toast;

public class StorageCheckor {

	public static boolean checkSdcard(Context context) {
		boolean isToast = (null != context);
		if (StringUtils.isEmpty(getExternalMemoryPath())) {
			if (isToast)
				Toast.makeText(context, "没有找到SD卡", 1).show();
			return false;
		}
		if (getAvailableExternalMemorySize() <= 0) {
			if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.FROYO) {
				return true;
			}
			if (isToast)
				Toast.makeText(context, "SD卡已无剩余空间", 1).show();
			return false;
		}
		return true;
	}

	public static String getExternalMemoryPath() {
		String state = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(state)) {
			File file = Environment.getExternalStorageDirectory();
			if (null == file)
				return "";
			if (file.exists())
				return file.getAbsolutePath();
		}
		return "";
	}

	public static long getTotalInternalMemorySize() {
		File path = Environment.getDataDirectory();
		StatFs stat = new StatFs(path.getPath());
		long blockSize = stat.getBlockSize();
		long totalBlocks = stat.getBlockCount();
		return totalBlocks * blockSize;
	}

	public static long getAvailableInternalMemorySize() {
		File path = Environment.getDataDirectory();
		StatFs stat = new StatFs(path.getPath());
		long blockSize = stat.getBlockSize();
		long availableBlocks = stat.getAvailableBlocks();
		return availableBlocks * blockSize;
	}

	public static long getAvailableExternalMemorySize() {
		long availableExternalMemorySize = 0;
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO) {
				File path = Environment.getExternalStorageDirectory();
				StatFs stat = new StatFs(path.getPath());
				long blockSize = stat.getBlockSize();
				long availableBlocks = stat.getAvailableBlocks();
				availableExternalMemorySize = availableBlocks * blockSize;
			}
		} else if (Environment.getExternalStorageState().equals(Environment.MEDIA_REMOVED)) {
			availableExternalMemorySize = -1;
		}
		return availableExternalMemorySize;
	}

	public static long getTotalExternalMemorySize() {
		long totalExternalMemorySize = 0;
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			File path = Environment.getExternalStorageDirectory();
			StatFs stat = new StatFs(path.getPath());
			long blockSize = stat.getBlockSize();
			long totalBlocks = stat.getBlockCount();
			totalExternalMemorySize = totalBlocks * blockSize;
		} else if (Environment.getExternalStorageState().equals(Environment.MEDIA_REMOVED)) {
			totalExternalMemorySize = -1;
		}
		return totalExternalMemorySize;
	}
}